package com.heima.article.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements AuthorService {
    @Override
    public ResponseResult insert(ApAuthor apAuthor) {
        if (ObjectUtil.isEmpty(apAuthor)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        apAuthor.setCreatedTime(new Date());

        save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findById(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<ApAuthor> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(ApAuthor::getId,id);
        ApAuthor author = getOne(lambdaQuery);
        return ResponseResult.okResult(author);
    }
}
